
package urbanparks;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * The function of this class is to enforce business rules
 * regarding number of jobs in a week. Also, this class is
 * used to display the date the job starts.
 * 
 * @author Ben Hadorn
 * @version Winter 2012
 */
public class JobDuration {
  /**
   * The length in days that the job lasts.
   */
  private final int my_job_length;
  /**
   * the date the job starts.
   */
  private final Calendar my_job_start_day;
  /**
   * a date formatter object to return human readable
   * strings from a Calendar object.
   */
  private final DateFormat my_date_formatter = new SimpleDateFormat(
      "MM-dd-yyyy");

  /**
   * a constructor that requires 4 arguments used to
   * populate the start and end dates of the job.
   * 
   * @param the_start_year an integer representing the year
   *          the job starts.
   * @param the_start_month an integer representing the
   *          month the job starts.
   * @param the_start_date an integer representing the date
   *          the job starts.
   * @param the_job_length an integer representing how many days the job is going to last.
   */
  public JobDuration(final int the_start_year,
                     final int the_start_month, 
                     final int the_start_date, 
                     int the_job_length) {
    my_job_start_day = Calendar.getInstance();
    my_job_start_day.set(Calendar.MONTH, the_start_month);
    my_job_start_day.set(Calendar.DAY_OF_MONTH, the_start_date);
    my_job_start_day.set(Calendar.YEAR, the_start_year);
    my_job_start_day.get(Calendar.YEAR);
    my_job_length = the_job_length;
    
  }

  /**
   * a method to get the day of the week the job starts
   * @return an integer representation of the day of the week.
   */
  public int getDay() {
    return my_job_start_day.get(Calendar.DAY_OF_MONTH);
  }
  /**
   * the number of days the job spans.
   * @return integer
   */
  public int getJobLength() {
    return my_job_length;
  }
  /**
   * to determine if the job is in the past we take the
   * start date of that job and subtract it from today
   * 
   * @return
   */
  public boolean isPastJob() {
    boolean past = false;
    if (my_job_start_day.getTimeInMillis() -
        Calendar.getInstance().getTimeInMillis() < 0) {
      past = true;
    }
    return past;
  }
  /**
   * returns an integer representation of the month the job
   * starts in.
   * @return an integer.
   */
  public int getMonth() {
    return my_job_start_day.get(Calendar.MONTH);
  }
  /**
   * returns an integer representation of the year the job
   * starts.
   * @return an integer.
   */
  public int getYear() {
    return my_job_start_day.get(Calendar.YEAR);
  }
  /**
   * returns a Date object representing the start time.
   * @return Date object.
   */
  public Date getStartTime() {
    return my_job_start_day.getTime();
  }
  /**
   * returns an integer representing what day of the year the job starts on
   * @return an integer
   */
  public int getDayOfYear() {
    return my_job_start_day.get(Calendar.DAY_OF_YEAR);
  }
  /**
   * overrides the toString method to print out the date in a human readable format.
   */
  @Override
  public String toString() {
    return my_date_formatter.format(my_job_start_day.getTime());
  }
}
